package com.siemens.industrial.web.mvc.controller;

import com.siemens.industrial.model.partner.Device;
import com.siemens.industrial.utils.ExportExcel;
import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.web.mvc.service.ExportDataFormatService;
import com.siemens.industrial.web.mvc.service.admin.ChartsService;
import com.siemens.industrial.web.mvc.service.admin.TenantService;
import com.siemens.industrial.web.mvc.service.partner.PartnerChartsService;
import com.siemens.industrial.web.mvc.service.partner.PartnerDeviceService;
import com.siemens.industrial.web.mvc.service.partner.PartnerDeviceStatePeriodService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shuan on 2016/12/30.
 */
@Controller
public class ExportDataFormatController {
    @Resource
    ExportDataFormatService exportDataFormatService;

    @Resource
    TenantService tenantService;

    @Resource
    ChartsService chartsService;

    @Resource
    PartnerChartsService partnerChartsService;

    @Resource
    PartnerDeviceStatePeriodService partnerDeviceStatePeriodService;

    @Resource
    PartnerDeviceService partnerDeviceService;

    //admin shift oee
    @RequestMapping(value = "/admin/export/shift/oee", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> adminShiftOee(@RequestBody Map<String, Object> requestParam) {
        try {
            List<Map<String, Object>> sheets = exportDataFormatService.dataFormatAdminShiftOee((ArrayList<Map<String, Object>>) requestParam.get("tenantAndDevice"), requestParam.get("day").toString(), requestParam.get("type").toString());
            String name = ExportExcel.exportExcel(sheets);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (IOException e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //partner shift oee
    @RequestMapping(value = "/partner/{tenant}/export/shift/oee", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> partnerShiftOee(@PathVariable("tenant") String tenantKey,
                                               @RequestParam(value = "day") String day,
                                               @RequestParam(value = "deviceIdArr[]") String[] deviceIdArr,
                                               @RequestParam(value = "type") String type) {
        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();

            List<Map<String, Object>> sheets = exportDataFormatService.getServicePartnerShiftOee(tenantId, deviceIdArr, day, type);
            String name = ExportExcel.exportExcel(sheets);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }


    //partner oee
    @RequestMapping(value = "/partner/{tenant}/export/oee", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> partnerOee(@PathVariable("tenant") String tenantKey,
                                          @RequestParam(value = "deviceIdArr[]") String[] deviceIdArr,
                                          @RequestParam(value = "startTime") String startTime,
                                          @RequestParam(value = "endTime") String endTime,
                                          @RequestParam(value = "type") String type) {
        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();

            Map<String, Object> list = partnerChartsService.serviceCompareNew(tenantId, deviceIdArr, startTime, endTime, type);
            List<Map<String, Object>> sheets = exportDataFormatService.dataFormatForPartnerOeeExport((ArrayList<Map<String, Object>>) list.get("serviceData"), deviceIdArr, type);
            String name = ExportExcel.exportExcel(sheets);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(new HashMap<>()).build();
        }
    }

    @RequestMapping(value = "/admin/api/v1/OEECompareExport", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> oEECompareExport(@RequestBody Map<String, Object> requestParam) {
        try {
            List<Map<String, Object>> list = chartsService.serviceCompareNew((ArrayList<Map<String, Object>>) requestParam.get("tenantAndDevice"), requestParam.get("startTime").toString(), requestParam.get("endTime").toString());
            List<Map<String, Object>> sheets = exportDataFormatService.dataFormatForAdminOeeExport(list, (List<Map<String, Object>>) requestParam.get("tenantAndDevice"));
            String name = ExportExcel.exportExcel(sheets);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //admin shift free
    @RequestMapping(value = "admin/api/v1/export/shiftFreeTimeAdmin", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> shiftFreeTimeAdminExport(@RequestParam(value = "tenant") String tenantId,
                                                        @RequestParam(value = "startTime") String startTime,
                                                        @RequestParam(value = "endTime") String endTime,
                                                        @RequestParam(value = "day") String day,
                                                        @RequestParam(value = "deviceId") String deviceId,
                                                        @RequestParam(value = "deviceName") String deviceName) {
        try {
            Map<String, Object> map = partnerChartsService.getShiftDeviceFreeInfo(tenantId, day, startTime, endTime, deviceId);
            List<Map<String, Object>> sheets = exportDataFormatService.shiftFreeTimeDataFormat(deviceName, day, startTime, endTime, (List<Map<String, Object>>) map.get("dataSource"));
            String name = ExportExcel.exportExcel(sheets);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //partner shift free
    @RequestMapping(value = "/partner/{tenant}/export/shiftFreeTimePartner", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> shiftFreeTimePartnerExport(@PathVariable("tenant") String tenantKey,
                                                          @RequestParam(value = "startTime") String startTime,
                                                          @RequestParam(value = "endTime") String endTime,
                                                          @RequestParam(value = "day") String day,
                                                          @RequestParam(value = "deviceId") String deviceId,
                                                          @RequestParam(value = "deviceName") String deviceName) {

        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();

            Map<String, Object> map = partnerChartsService.getShiftDeviceFreeInfo(tenantId, day, startTime, endTime, deviceId);
            List<Map<String, Object>> sheets = exportDataFormatService.shiftFreeTimeDataFormat(deviceName, day, startTime, endTime, (List<Map<String, Object>>) map.get("dataSource"));
            String name = ExportExcel.exportExcel(sheets);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //admin dayOrShiftState
    @RequestMapping(value = "/admin/api/v1/export/dayOrShiftState", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDayOrShiftStateAdminData(@RequestParam(value = "tenant") String tenantId,
                                                           @RequestParam(value = "deviceId") String deviceId,
                                                           @RequestParam(value = "deviceName") String deviceName,
                                                           @RequestParam(value = "day") String day,
                                                           @RequestParam(value = "startTime") String startTime,
                                                           @RequestParam(value = "endTime") String endTime) {
        try {
            List<Map<String, Object>> shiftStates = partnerDeviceStatePeriodService.getOneShiftState(tenantId, day, startTime, endTime, deviceId);

            List<Map<String, Object>> sheets = exportDataFormatService.dayOrShiftAdminState(deviceName, day, startTime, endTime, shiftStates);
            String name = ExportExcel.exportExcel(sheets);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //partner dayOrShiftState
    @RequestMapping(value = "/partner/{tenant}/api/v1/export/dayOrShiftState", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDayShiftStatePartnerData(@PathVariable("tenant") String tenantKey,
                                                           @RequestParam(name = "deviceId") String deviceId,
                                                           @RequestParam(value = "deviceName") String deviceName,
                                                           @RequestParam(name = "day") String day,
                                                           @RequestParam(name = "startTime") String startTime,
                                                           @RequestParam(name = "endTime") String endTime) {

        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        try {
            List<Map<String, Object>> shiftStates = partnerDeviceStatePeriodService.getOneShiftState(tenantId, day, startTime, endTime, deviceId);

            List<Map<String, Object>> sheets = exportDataFormatService.dayOrShiftAdminState(deviceName, day, startTime, endTime, shiftStates);
            String name = ExportExcel.exportExcel(sheets);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.SUCCESS).build();
        }
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/export/sameDeviceDiffReasonExport", method = RequestMethod.GET)
    @ResponseBody
    public Map getSameDeviceDiffTypeTimeAndAllType(@PathVariable("tenant") String tenantKey,
                                                   @RequestParam(value = "device") String device,
                                                   @RequestParam(value = "startTime") String startTime,
                                                   @RequestParam(value = "endTime") String endTime) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        try {
            Map map = partnerChartsService.getSameDeviceDiffTypeTime(tenantId, device, startTime, endTime);
            List<String> reasons = (List<String>) map.get("reasonList");
            List<Map<String, Object>> datas = (List<Map<String, Object>>) map.get("idleInfo");
            List<Map<String, Object>> sheets = exportDataFormatService.dataFormatForDiffReasonExport(reasons, datas);
            Device tanDevice = partnerDeviceService.getDevice(tenantId, device);
            String name = ExportExcel.exportExcel(sheets, tanDevice.getDeviceUniqueId());
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/export/sameDeviceSameReason", method = RequestMethod.GET)
    @ResponseBody
    public Map getSameDeviceSameTypeTime(@PathVariable("tenant") String tenantKey,
                                         @RequestParam(value = "device") String device,
                                         @RequestParam(value = "reason") String reason,
                                         @RequestParam(value = "reasonName") String reasonName,
                                         @RequestParam(value = "startTime") String startTime,
                                         @RequestParam(value = "endTime") String endTime) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        try {
            Map map = partnerChartsService.getSameDeviceSameTypeTime(tenantId, device, reason, reasonName, startTime, endTime);
            Device sieDevice = partnerDeviceService.getDevice(tenantId, device);
            List<Map<String, Object>> sheets = (List<Map<String, Object>>) map.get("idleInfo");
            String name = ExportExcel.exportExcel(sheets, sieDevice.getDeviceUniqueId());
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/export/sameReasonDevices", method = RequestMethod.GET)
    @ResponseBody
    public Map getSameReasonDevices(@PathVariable("tenant") String tenantKey,
                                    @RequestParam(value = "reason") String reason,
                                    @RequestParam(value = "reasonName") String reasonName,
                                    @RequestParam(value = "startTime") String startTime,
                                    @RequestParam(value = "endTime") String endTime) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        try {
            Map map = partnerChartsService.getSameReasonDevices(tenantId, reason, reasonName, startTime, endTime);
            List<String> MTids = (List<String>) map.get("series");
            List<Map<String, Object>> datas = (List<Map<String, Object>>) map.get("idleList");
            List<Map<String, Object>> sheets = exportDataFormatService.dataFormatForSameReasonExport(MTids, datas);
            String name = ExportExcel.exportExcel(sheets, map.get("reason").toString());
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        }catch (Exception e){
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //admin deviceStateChart
    @RequestMapping(value = "/admin/api/v1/export/deviceState", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deviceStateList(@RequestBody Map<String, Object> requestParam) {
        try {
            Map<String, List<Map<String, Object>>> map = chartsService.stateBuild(requestParam);

            List<Map<String,Object>> sheets = exportDataFormatService.deviceStateFormat((List<Map<String, Object>>) requestParam.get("tenantAndDevice"),map.get("dataSource"));
            String name = ExportExcel.exportExcel(sheets);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //partner deviceStateChart
    @RequestMapping(value = "/partner/{tenant}/api/v1/export/deviceStateMore", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getDeviceStateChart(@RequestBody Map<String,Object> requestParam,
                                                   @PathVariable("tenant") String tenantKey) {
        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
            Map<String, List<Map<String,Object>>> map = partnerDeviceStatePeriodService.dataFormatOfMore(tenantId, (List<Map<String,Object>>)requestParam.get("deviceArr"),requestParam.get("startTime").toString(),requestParam.get("endTime").toString());
            List<Map<String,Object>> sheets = exportDataFormatService.deviceStateFormat((List<Map<String,Object>>)requestParam.get("deviceArr"),map.get("dataSource"));
            String name = ExportExcel.exportExcel(sheets);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        }catch (Exception e){
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

}
